(function(vc) {
    vc.extends({
        data: {
            stockUpStoreOrderInfo: {
                orderId: '',
                appId: '',
                appName: '',
                deduction: '',
                personName: '',
                stateName: '',
                profitSharing: '',
                payPrice: '',
                createTime: '',
                quan: false,
                addressInfo: {
                    username: '',
                    tel: '',
                    address: ''
                },
                remark: '',
                storeOrderCarts: [],
                selectCarts: [],
                extOrderId:'',
                stockUpRemark:'备货完成'
            }
        },
        _initMethod: function() {
            $that.stockUpStoreOrderInfo.orderId = vc.getParam('orderId');
            $that._loadStoreOrder();
            $that._listOrderCart();
            $that._listOrderWriteOff();
        },
        _initEvent: function() {},
        watch: { // 监视双向绑定的数据数组
            stockUpStoreOrderInfo: {
                handler() { // 数据数组有变化将触发此函数
                    if ($that.stockUpStoreOrderInfo.selectCarts.length == $that.stockUpStoreOrderInfo.storeOrderCarts.length) {
                        $that.stockUpStoreOrderInfo.quan = true;
                    } else {
                        $that.stockUpStoreOrderInfo.quan = false;
                    }
                },
                deep: true // 深度监视
            }
        },
        methods: {
            _loadStoreOrder: function() {
                let param = {
                    params: {
                        page: 1,
                        row: 1,
                        orderId: $that.stockUpStoreOrderInfo.orderId
                    }
                };
                //发送get请求
                vc.http.apiGet('/storeOrder.queryStoreOrder',
                    param,
                    function(json, res) {
                        let _json = JSON.parse(json);
                        vc.copyObject(_json.data[0], $that.stockUpStoreOrderInfo)
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listOrderCart: function() {
                let param = {
                    params: {
                        page: 1,
                        row: 1,
                        orderId: $that.stockUpStoreOrderInfo.orderId,
                        shopId:vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/storeOrder.queryStoreOrderCart',
                    param,
                    function(json, res) {
                        let _json = JSON.parse(json);
                        let _storeOrderCart = _json.data
                        _storeOrderCart.forEach(_orderCart => {
                            if (_orderCart.productSpecDetails) {
                                let _productSpecDetails = _orderCart.productSpecDetails;
                                let _specValue = '';
                                _productSpecDetails.forEach(detail => {
                                    _specValue += (detail.detailValue + "/");
                                });
                                _orderCart.specValue = _specValue;
                            }
                        })
                        $that.stockUpStoreOrderInfo.storeOrderCarts = _storeOrderCart;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _listOrderWriteOff: function() {
                let param = {
                    params: {
                        page: 1,
                        row: 1,
                        orderId: $that.stockUpStoreOrderInfo.orderId,
                        shopId:vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/storeOrder.listStoreOrderWriteoff',
                    param,
                    function(json, res) {
                        let _json = JSON.parse(json);
                        $that.stockUpStoreOrderInfo.extOrderId = _json.data[0].woNum;
                    },
                    function(errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _goBack: function() {
                vc.goBack()
            },
            checkAllCart: function (e) {
                let checkObj = document.querySelectorAll('.checkCartItem'); // 获取所有checkbox项
                if (e.target.checked) { // 判定全选checkbox的勾选状态
                    for (var i = 0; i < checkObj.length; i++) {
                        if (!checkObj[i].checked) { // 将未勾选的checkbox选项push到绑定数组中
                            let _value = checkObj[i].value;
                            $that.stockUpStoreOrderInfo.selectCarts.push(_value);
                        }
                    }
                } else { // 如果是去掉全选则清空checkbox选项绑定数组
                    $that.stockUpStoreOrderInfo.selectCarts = [];
                }
            },
            _minusCartNum:function(_cart){
                _cart.cartNum -=1;
                if(_cart.cartNum < 1){
                    _cart.cartNum = 1;
                }
            },
            _finishStockUpOrder: function () {

                let _selectCarts = $that.stockUpStoreOrderInfo.selectCarts;
                if(_selectCarts.length < 1){
                    vc.toast("请选择商品");
                    return ;
                }

                let _storeOrderCarts = $that.stockUpStoreOrderInfo.storeOrderCarts;
                let _carts = [];
                _storeOrderCarts.forEach(_c=>{
                    _selectCarts.forEach(_s=>{
                        if(_s == _c.cartId){
                            _carts.push(_c);
                        }
                    });
                });
                if(_carts.length < 1){
                    vc.toast("请选择商品");
                    return ;
                }
                let _data = {
                    shopId:vc.getCurrentCommunity().shopId,
                    orderId:$that.stockUpStoreOrderInfo.orderId,
                    carts:_carts,
                    extOrderId:$that.stockUpStoreOrderInfo.extOrderId,
                    stockUpRemark:$that.stockUpStoreOrderInfo.stockUpRemark,
                }
                vc.http.apiPost(
                    '/storeOrder.stockUpStoreOrder',
                    JSON.stringify(_data), {
                    emulateJSON: true
                },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.goBack();
                            vc.toast("添加成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
        }
    });
})(window.vc);